/*
 * Configuracio.java
 *
 * Created on 17 de diciembre de 2007, 16:23
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.wooxes.server;

//import org.wooxes.client.Configuracio;
import java.sql.*;
/**
 *
 * @author woox
 */
public class ConfiguracioServidor{
    private Connection conn;
    
    /** Creates a new instance of Configuracio */
    public ConfiguracioServidor() {
        ConnexioSQL connexio = new ConnexioSQL();
        conn = connexio.getConnection();
    }

    public ConfiguracioServidor(Connection connection) {
        conn = connection;
    }
    
    public String getUploadDir(){
        String dir=null;
        dir = buscar("uploaddir");
        return dir;
    }
   
    public int getMaxFileSize(){      
        return getMaxFileSize("b");
    }
    
    public int getMaxFileSize(String format){
        int size = 0;

        size = Integer.parseInt(buscar("maxfilesize"));
        
        if(size == 0){
            return size;
        }
        
        if(format.equals("b")){
            size = size * 1024;
        }
        else if (format.equals("kb")){
            // res a fer, ja esta en KB
        }
        else if (format.equals("mb")){
            size = size / 1024;
        }
        
        return size;
    }
    

    private String buscar(String st1){
        String result = null;
        
        try{
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM configuracio WHERE nom='"+st1+"'");
            while( rs.next() ){
                result = rs.getString("valor");
            }
        }
        catch(SQLException e){
            System.out.println(e);
        }
        
        return result;
    }
}
